package com.slj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slj.comm.responce.R;
import com.slj.model.dto.EmDTO;
import com.slj.model.entity.Employees;
import com.slj.mapper.EmployeesMapper;
import com.slj.service.EmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 员工登录表 服务实现类
 * </p>
 *
 * @author 礼俊
 * @since 2025-09-20 22:46
 */
@Service
@Slf4j
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees> implements EmployeesService {
    @Autowired
    private EmployeesMapper employeesMapper;

    /**
     * 分页查询员工数据
     *
     * @return
     */
    @Override
    public Page<Employees> pageList(EmDTO  emDTO) {
        LambdaQueryWrapper<Employees> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Employees::getEmpName, emDTO.getEmpName());
        queryWrapper.like(Employees::getPhoneNumber, emDTO.getPhoneNumber());
        Page<Employees> page = new Page<>(emDTO.getPageNum(), emDTO.getPageSize());
        return employeesMapper.selectPage(page, queryWrapper);
    }

    /**
     * 编辑员工
     *
     * @param employees
     * @return
     */
    @Override
    public R editEmloyees(Employees employees) {
        if (employees.getEmployeeId() == null){
            log.info("编辑员工");
            // 检查 login_name 是否已存在
            QueryWrapper<Employees> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("login_name", employees.getLoginName());
            boolean exists = employeesMapper.exists(queryWrapper);
            if (exists){
                return R.fail("用户名已存在！");
            }else {
                employees.setPassword("123456");
                return R.success(employeesMapper.insert(employees));
            }
        }else{
            return R.success(employeesMapper.updateById(employees));
        }
    }
}
